package com.xingfu.mybatisdemo.config;

import com.github.pagehelper.PageInterceptor;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.jta.JtaTransactionManager;

import javax.sql.DataSource;
import java.io.IOException;
import java.util.Properties;

/**
 * @author zhangx & jietuo_zx@163.com
 * @version 1.0
 * @date 2019-09-20
 * @description:
 */
@Configuration
@EnableTransactionManagement
public class SessionFactoryConfig {

    @Bean(name = "writeDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.write")
    @Primary
    public DataSource writeDataSource() {
        System.out.println("writeDataSource build");
        return DataSourceBuilder.create().build();
    }

    @Bean(name = "readDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.read")
    public DataSource readDataSource() {
        System.out.println("readDataSource build");
        return DataSourceBuilder.create().build();

    }


    @Bean(name = "readSessionFactory")
    public SqlSessionFactoryBean readSessionFactory(@Qualifier(
            "readDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

        //设置分页插件
        Interceptor interceptor = new PageInterceptor();
        Properties properties = new Properties();
        //配置数据库类型
        properties.setProperty("helperDialect","mysql");
        //是否分页合理化
        properties.setProperty("reasonable", "false");
        interceptor.setProperties(properties);
        sqlSessionFactoryBean.setPlugins(new Interceptor[]{interceptor});

        //设置数据源
        sqlSessionFactoryBean.setDataSource(dataSource);
        //设置别名
//        sqlSessionFactoryBean.setTypeAliasesPackage("com.xingfu.mybatisdemo.persistence.po");

        //设置mapper.xml文件
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mapper/read/*.xml"));
        return sqlSessionFactoryBean;
    }

    @Bean(name = "writeSessionFactory")
    @Primary
    public SqlSessionFactoryBean writeSessionFactory(@Qualifier(
            "writeDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();

        //设置分页插件
        Interceptor interceptor = new PageInterceptor();
        Properties properties = new Properties();
        //配置数据库类型
        properties.setProperty("helperDialect","mysql");
        //是否分页合理化
        properties.setProperty("reasonable", "false");
        interceptor.setProperties(properties);
        sqlSessionFactoryBean.setPlugins(new Interceptor[]{interceptor});

        //设置数据源
        sqlSessionFactoryBean.setDataSource(dataSource);
        //设置别名
//        sqlSessionFactoryBean.setTypeAliasesPackage("com.xingfu.mybatisdemo.persistence.po");
        //设置mapper.xml文件
        sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver()
                .getResources("classpath:mapper/write/*.xml"));
        return sqlSessionFactoryBean;
    }

    @Bean(name = "writeMapperScannerConfigurer")
    public MapperScannerConfigurer writeMapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.xingfu.mybatisdemo.persistence.mapper");
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("writeSessionFactory");
        return mapperScannerConfigurer;
    }

    @Bean(name = "readMapperScannerConfigurer")
    public MapperScannerConfigurer readMapperScannerConfigurer(){
        MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
        mapperScannerConfigurer.setBasePackage("com.xingfu.mybatisdemo.persistence.dao");
        mapperScannerConfigurer.setSqlSessionFactoryBeanName("readSessionFactory");
        return mapperScannerConfigurer;
    }

    @Primary
    @Bean(name = "writeTransactionManager")
    public PlatformTransactionManager writeTransactionManager(@Qualifier("writeDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}
